package org.example.snow.demo5;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * @author snowsong
 */
public class KafkaTestProducer {
    public static void main(String[] args) throws InterruptedException {
        Properties props = new Properties();
        // Kafka 集群的初始连接地址
        props.put("bootstrap.servers", "172.16.1.173:9092");
        // 序列化器 将 Java 对象序列化为字节数组
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        // kafka生产者
        KafkaProducer<String, String> producer = new KafkaProducer<>(props);
        // 消息循环
        for (int i = 0; i < 50; i++) {
            String key = "key-" + i;
            String value = "value-" + i;
            ProducerRecord<String, String> record = new ProducerRecord<>("xue", key, value);
            producer.send(record);
            System.out.println("send: " + key);
            Thread.sleep(200);
        }
        // 关闭生产者
        producer.close();
    }
}
